{% extends "admin/admin_layout.html" %}
{% block admin_title %}管理权限{% endblock %}
{% block admin_head %}
{% endblock %}
{% set perm = request.perm_const %}
{% block admin_content %}
<div class="sect">
	<h2>权限列表
    	<span class="sub">共{{ page.paginator.count }}个权限</span>
        <span class="extra">
        	{% if perm.check_perm_op(perm.perm_perm,perm.op_add) %}
        	<a href="javascript:;" class="act_add" onclick="add_perm(event);">添加权限</a>
        	{% endif %}
        </span>
    </h2>
    <div class="sectContent">
    	<table id="perm_table" class="gTable">
        	<tr>
            	<th class="m1"></th>
                <th class="m4">ID</th>
                <th class="m0">权限名</th>
                <th class="m6">相关操作</th>
            	<th class="m1"></th>
                <th class="m4">ID</th>
                <th class="m0">权限名</th>
                <th class="m6">相关操作</th>
            </tr>
            <tr>
            {% for obj in page.object_list %}
            	{% if loop.index % 2 == 1 %}
                </tr><tr>
                {% endif %}
            	<td class="m1">
                	<input type="checkbox" autocomplete="off" value="{{ obj.id }}"  id="chk_{{ obj.id }}" />
                </td>
                <td class="m4">{{ obj.id }}</td>
                <td>
                	<a href="javascript:;" onclick="edit_perm(event,'{{ obj.id }}','{{ obj.name }}');">{{ obj.name }}</a>
                	(<span id="op_n_{{obj.id}}" class="tips">{{obj.getOpNames()}}</span>)
                </td>
                <td class="m6">
                	{% if perm.check_perm_op(perm.perm_perm,perm.op_change) %}
					<a href="javascript:;" class="action" onclick="show_ops('{{obj.id}}');">相关操作</a>
					{% endif %}
					<input type="hidden" id="op_v_{{obj.id}}" value="{{obj.getOpIds()}}"  />
                </td>
            {% endfor %}
            </tr>
            <tr>
            	<td><input type="checkbox" autocomplete="off" id="chkAll" onclick="$checkAll('perm_table','chkAll');" /></td>
                <td colspan="7">
                	{% if perm.check_perm_op(perm.perm_perm,perm.op_del) %}
                	<input type="button" class="button btns" value="删除选中" onclick="del_perm('perm_table','chkAll');" />
                	{% endif %}
                </td>
            </tr>
        </table>
        {% include "paginator.html" %}
    </div>
</div>
{% endblock %}
{% block popup_content %}
<div id="s_perm_div" class="popup" style="display:none;">
	<div class="sect">
    	<h2>
        	<span id="s_perm_title" class="title"></span>
            <span class="extra" onclick="$('s_perm_div').tHide();">关闭</span>
        </h2>
        <div class="sectContent gForm">
        	<div id="id_row" class="row">
            	<label>权限ID：</label>
                <input type="text" class="inputbox" id="perm_id" />
                <input type="hidden" id="perm_former_id" />
            </div>
        	<div id="name_row" class="row">
            	<label>权限名：</label>
                <input type="text" class="inputbox" id="perm_name" />
            </div>
            <div class="sectBtn">
            	<input type="button" class="button" value="确定" onclick="do_perm();" />
                <a href="javascript:;" onclick="$('s_perm_div').tHide();">取消</a>
            </div>
        </div>
    </div>
</div>
<div id="s_op_div" class="popup popmiddle" style="display:none;">
    <div class="sect">
        <h2>
            <span id="s_opy_title" class="title">操作列表</span>
            <span class="extra" onclick="$('s_op_div').tHide();">关闭</span>
        </h2>
        <div class="sectContent gForm gfms">
            <div class="row">
            	<table id="op_table" class="sTable">
                	<tr>
                	{% for op in operations %}
                        <td><input type="checkbox" autocomplete="off" {{ "id='chk_%d' value='%s'"|format(op.id,op.name) }}
                        	onclick="check_op();" />{{ op.name }}
                        </td>
                        {% if loop.index % 3 == 0 %}</tr><tr>{% endif %}
                    {% endfor %}
                    </tr>
                </table>
            </div>
            <div class="row">
                <input type="button" class="buttonLarge" value="确定" onclick="do_ops();" />
                <input type="hidden" value="" id="hide_permid" />
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
function add_perm(ev){
    $("s_perm_title").innerHTML = "添加权限";$("perm_former_id").value = "";$("perm_id").value = "";$("perm_name").value = "";
    $('s_perm_div').fshow({event:ev});
}
function edit_perm(ev, id, name){
    $("s_perm_title").innerHTML = "修改权限";$("perm_former_id").value = id;$("perm_id").value = id;$("perm_name").value = name;
    $('s_perm_div').fshow({event:ev});
}
function do_perm(){
    var to_url = "";var former_id = $("perm_former_id").value;var id = $("perm_id").value;
	var name = $("perm_name").value;var params = "";
    if (id == null || id == undefined || id == "") {new MsgBox({message:"你还未输入权限ID"});return;}
    if (name == null || name == undefined || name == "") {new MsgBox({message:"你还未输入权限名"});return;}
    params = String.format("id={0}&name={1}&former_id={2}", id, String.encode(name), former_id);
    new echoAjax({url:"/admin/perm/do/", params:params,reLoad:true}).fire();
}
function del_perm(parent, chkall){
    if (!confirm("你确定要删除选中的内容吗？"))return;
    var ids = $getCheckValues(parent, chkall);
    if (!ids) {new MsgBox({message:"没有选中任何内容"});return;}
    var params = "value=" + ids;
    new echoAjax({url:"/admin/perm/delete/", params:params,reLoad:true}).fire();
}
function check_op(){
	var perm_id = $("hide_permid").value;
	$checkMe('op_table','op_n_'+perm_id,'op_v_'+perm_id);
}
function show_ops(perm_id){
	$("hide_permid").value = perm_id;
	$showInfoTable("s_op_div","op_n_" + perm_id);
}
function do_ops(){
	var perm_id = $("hide_permid").value;
	var op_ids = $("op_v_" + perm_id).value;
	var params = "value=" + op_ids;
	new echoAjax({url:"/admin/perm/"+perm_id+"/do_op/",params:params,message:{success:'操作成功'}}).fire();
	$('s_op_div').fshow();
}
</script>
{% endblock %}
